package com.yixun.qfbao.hander;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yixun.qfbao.api.service.WxTemplateMsgService;
import com.yixun.qfbao.api.service.impl.WxTemplateMsgServiceImpl;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.CheckRecord;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.dto.PartnerInfoDto;
import com.yixun.qfbao.model.enums.CheckStatusEnum;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.model.enums.ModelTypeEnum2;
import com.yixun.qfbao.service.CheckRecordService;
import com.yixun.qfbao.service.PartnerInfoService;
import com.yixun.qfbao.vo.PartnerCheckReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

/**
 * @author zilong.liu
 * @date 2020-12-16
 *
 * desc:
 *     	人员审核
 */
@Service("partnerCheckHander")
public class PartnerCheckHander extends CommonAbstractHander{

	
	@Autowired
	CheckRecordService checkRecordService;
	
	@Autowired
	PartnerInfoService partnerInfoService;
	
	@Autowired
	WxTemplateMsgService wxTemplateMsgService;
	
	@Override
	@Transactional
	public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
		BaseResponseDTO<String> res = new BaseResponseDTO<>();
		
		
		PartnerCheckReqVo partnerCheckReqVo = (PartnerCheckReqVo)reqDto;
		long checkUserId = partnerCheckReqVo.getParams().getCheckUserId();
		String companyUuid = partnerCheckReqVo.getParams().getCompanyUuid();
		// 检查当前人员状态，避免多次审核
		PartnerInfo partnerInfo = partnerInfoService.selectById(checkUserId);
		if(!CheckStatusEnum.TODO.getNo().equals(partnerInfo.getCheckStatus())) {
			res.setErrorMsg("已审核，请勿重复审核");
			res.setResult(ResultEnum.FAIL.getNo());
			return res;
		}
		
		String checkStatus = partnerCheckReqVo.getParams().getCheckStatus();
		String userId = partnerCheckReqVo.getParams().getUserId();
		String remark = partnerCheckReqVo.getParams().getRemark();
		
		
		CheckRecord checkRecord = new CheckRecord();
		checkRecord.setType(ModelTypeEnum2.PARTNER_CHECK.getNo());
		checkRecord.setRelId(checkUserId);
		checkRecord.setRemark(remark);
		checkRecord.setCreateUserId(userId);
		checkRecord.setStatus(checkStatus);
		checkRecordService.add(checkRecord);
		
		PartnerInfoDto entity = new PartnerInfoDto();
		
		entity.setId(checkUserId);
		entity.setCheckStatus(checkStatus);
		entity.setCheckRecordId(checkRecord.getId());
		// 如果是审核失败，那么直接设置为人员删除
		if(CheckStatusEnum.FAILURE.getNo().equals(checkStatus)) {
			entity.setDelFlag(DelFlagEnum.DEL.getNo());
		}
		partnerInfoService.update(entity);
		
		// 下发审核消息，审核成功和失败消息不一致
		wxTemplateMsgService.sendCheckTemplateMsg(companyUuid, partnerInfo, 
				CheckStatusEnum.SUCCESS.getNo().equals(checkStatus), 
				remark, WxTemplateMsgServiceImpl.CHECK_TYPE_ARRAY[0]);
		
		res.setResData("成功");
		res.setResult(ResultEnum.SUCCESS.getNo());
		return res;
	}

}
